import fs from "fs";
import path from "path";
import { encryptUTF8 } from "siphan";
import { addFilename, safeWrite } from './utils';

function encrypt(data: string, fileAddress: string) {
	const { cipher, solution } = encryptUTF8(data);
	safeWrite(addFilename(fileAddress, "-cipher"), cipher);
	safeWrite(addFilename(fileAddress, "-key"), solution);
}

export default function encryptMain(fileAddress: string) {
	fs.readFile(path.resolve(process.cwd(), fileAddress), (err, data) => {
		if (err !== null) {
			throw err;
		}
		encrypt(data.toString(), fileAddress);
	});
}
